{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# text to image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# Vincent diagram example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "\n",
    "def text2img(params: dict) -> dict:\n",
    "    \"\"\"\n",
    "    Vincentian picture\n",
    "    \"\"\"\n",
    "    result = requests.post(url=f\"{host}/v1/generation/text-to-image\",\n",
    "                           data=json.dumps(params),\n",
    "                           headers={\"Content-Type\": \"application/json\"})\n",
    "    return result.json()\n",
    "\n",
    "result =text2img(\n",
    "        {\"prompt\": \"1girl sitting on the ground\",\n",
    "         \"async_process\": True}\n",
    "    )\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# upscale or vary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "\n",
    "# upscale or vary v1 Interface example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "\n",
    "def upscale_vary(image, params: dict) -> dict:\n",
    "    \"\"\"\n",
    "    Upscale or Vary\n",
    "    \"\"\"\n",
    "    response = requests.post(url=f\"{host}/v1/generation/image-upscale-vary\",\n",
    "                        data=params,\n",
    "                        files={\"input_image\": image})\n",
    "    return response.json()\n",
    "\n",
    "result =upscale_vary(image=image,\n",
    "                     params={\n",
    "                         \"uov_method\": \"Upscale (2x)\",\n",
    "                         \"async_process\": True\n",
    "                     })\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import base64\n",
    "\n",
    "\n",
    "# upscale or vary v2 Interface example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "\n",
    "def upscale_vary(params: dict) -> dict:\n",
    "    \"\"\"\n",
    "    Upscale or Vary\n",
    "    \"\"\"\n",
    "    response = requests.post(url=f\"{host}/v2/generation/image-upscale-vary\",\n",
    "                        data=json.dumps(params),\n",
    "                        headers={\"Content-Type\": \"application/json\"},\n",
    "                        timeout=300)\n",
    "    return response.json()\n",
    "\n",
    "result =upscale_vary(params={\n",
    "                         \"input_image\": base64.b64encode(image).decode('utf-8'),\n",
    "                         \"uov_method\": \"Upscale (2x)\",\n",
    "                         \"async_process\": True\n",
    "                     })\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# inpaint or outpaint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# Partial redraw v1 interface example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "\n",
    "def inpaint_outpaint(params: dict, input_image: bytes, input_mask: bytes = None) -> dict:\n",
    "    \"\"\"\n",
    "    Partial redraw v1 interface example\n",
    "    \"\"\"\n",
    "    response = requests.post(url=f\"{host}/v1/generation/image-inpaint-outpaint\",\n",
    "                        data=params,\n",
    "                        files={\"input_image\": input_image,\n",
    "                               \"input_mask\": input_mask})\n",
    "    return response.json()\n",
    "\n",
    "\n",
    "# Image extension example\n",
    "result = inpaint_outpaint(params={\n",
    "                            \"outpaint_selections\": \"Left,Right\",\n",
    "                            \"async_process\": True},\n",
    "                          input_image=image,\n",
    "                          input_mask=None)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Partial redraw example\n",
    "source = open(\"./imgs/s.jpg\", \"rb\").read()\n",
    "mask = open(\"./imgs/m.png\", \"rb\").read()\n",
    "result = inpaint_outpaint(params={\n",
    "                            \"prompt\": \"a cat\",\n",
    "                            \"async_process\": True},\n",
    "                          input_image=source,\n",
    "                          input_mask=mask)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import base64\n",
    "\n",
    "\n",
    "# Partial redraw v2 interface example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "\n",
    "def inpaint_outpaint(params: dict) -> dict:\n",
    "    \"\"\"\n",
    "    Partial redraw v2 interface example\n",
    "    \"\"\"\n",
    "    response = requests.post(url=f\"{host}/v2/generation/image-inpaint-outpaint\",\n",
    "                        data=json.dumps(params),\n",
    "                        headers={\"Content-Type\": \"application/json\"})\n",
    "    return response.json()\n",
    "\n",
    "# Image extension example\n",
    "result = inpaint_outpaint(params={\n",
    "                            \"input_image\": base64.b64encode(image).decode('utf-8'),\n",
    "                            \"input_mask\": None,\n",
    "                            \"outpaint_selections\": [\"Left\", \"Right\"],\n",
    "                            \"async_process\": True})\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Partial redraw example\n",
    "source = open(\"./imgs/s.jpg\", \"rb\").read()\n",
    "mask = open(\"./imgs/m.png\", \"rb\").read()\n",
    "result = inpaint_outpaint(params={\n",
    "                            \"prompt\": \"a cat\",\n",
    "                            \"input_image\": base64.b64encode(source).decode('utf-8'),\n",
    "                            \"input_mask\": base64.b64encode(mask).decode('utf-8'),\n",
    "                            \"async_process\": True})\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# image prompts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "\n",
    "# image_prompt v1 Interface example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "source = open(\"./imgs/s.jpg\", \"rb\").read()\n",
    "mask = open(\"./imgs/m.png\", \"rb\").read()\n",
    "\n",
    "def image_prompt(params: dict,\n",
    "                 input_iamge: bytes=None,\n",
    "                 input_mask: bytes=None,\n",
    "                 cn_img1: bytes=None,\n",
    "                 cn_img2: bytes=None,\n",
    "                 cn_img3: bytes=None,\n",
    "                 cn_img4: bytes=None,) -> dict:\n",
    "    \"\"\"\n",
    "    image prompt\n",
    "    \"\"\"\n",
    "    response = requests.post(url=f\"{host}/v1/generation/image-prompt\",\n",
    "                             data=params,\n",
    "                             files={\n",
    "                                 \"input_image\": input_iamge,\n",
    "                                 \"input_mask\": input_mask,\n",
    "                                 \"cn_img1\": cn_img1,\n",
    "                                 \"cn_img2\": cn_img2,\n",
    "                                 \"cn_img3\": cn_img3,\n",
    "                                 \"cn_img4\": cn_img4,\n",
    "                              })\n",
    "    return response.json()\n",
    "\n",
    "# image extension\n",
    "params = {\n",
    "    \"outpaint_selections\": [\"Left\", \"Right\"],\n",
    "    \"image_prompts\": [] # Required parameters, can be an empty list\n",
    "}\n",
    "result = image_prompt(params=params, input_iamge=image)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# partial redraw\n",
    "\n",
    "params = {\n",
    "    \"prompt\": \"1girl sitting on the chair\",\n",
    "    \"image_prompts\": [], # Required parameters, can be an empty list\n",
    "    \"async_process\": True\n",
    "}\n",
    "result = image_prompt(params=params, input_iamge=source, input_mask=mask)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# image prompt\n",
    "\n",
    "params = {\n",
    "    \"prompt\": \"1girl sitting on the chair\",\n",
    "    \"image_prompts\": [\n",
    "        {\n",
    "            \"cn_stop\": 0.6,\n",
    "            \"cn_weight\": 0.6,\n",
    "            \"cn_type\": \"ImagePrompt\"\n",
    "        },{\n",
    "            \"cn_stop\": 0.6,\n",
    "            \"cn_weight\": 0.6,\n",
    "            \"cn_type\": \"ImagePrompt\"\n",
    "        }]\n",
    "    }\n",
    "result = image_prompt(params=params, cn_img1=image, cn_img2=source)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import base64\n",
    "\n",
    "# image_prompt v2 Interface example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "source = open(\"./imgs/s.jpg\", \"rb\").read()\n",
    "mask = open(\"./imgs/m.png\", \"rb\").read()\n",
    "\n",
    "def image_prompt(params: dict) -> dict:\n",
    "    \"\"\"\n",
    "    image prompt\n",
    "    \"\"\"\n",
    "    response = requests.post(url=f\"{host}/v2/generation/image-prompt\",\n",
    "                             data=json.dumps(params),\n",
    "                             headers={\"Content-Type\": \"application/json\"})\n",
    "    return response.json()\n",
    "\n",
    "# image extension\n",
    "params = {\n",
    "    \"input_image\": base64.b64encode(image).decode('utf-8'),\n",
    "    \"outpaint_selections\": [\"Left\", \"Right\"],\n",
    "    \"image_prompts\": [] # Required parameters, can be an empty list\n",
    "}\n",
    "result = image_prompt(params)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# partial redraw\n",
    "\n",
    "params = {\n",
    "    \"prompt\": \"1girl sitting on the chair\",\n",
    "    \"input_image\": base64.b64encode(source).decode('utf-8'),\n",
    "    \"input_mask\": base64.b64encode(mask).decode('utf-8'),\n",
    "    \"image_prompts\": [], # Required parameters, can be an empty list\n",
    "    \"async_process\": True\n",
    "}\n",
    "result = image_prompt(params)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# image prompt\n",
    "\n",
    "params = {\n",
    "    \"prompt\": \"1girl sitting on the chair\",\n",
    "    \"image_prompts\": [\n",
    "        {\n",
    "            \"cn_img\": base64.b64encode(source).decode('utf-8'),\n",
    "            \"cn_stop\": 0.6,\n",
    "            \"cn_weight\": 0.6,\n",
    "            \"cn_type\": \"ImagePrompt\"\n",
    "        },{\n",
    "            \"cn_img\": base64.b64encode(image).decode('utf-8'),\n",
    "            \"cn_stop\": 0.6,\n",
    "            \"cn_weight\": 0.6,\n",
    "            \"cn_type\": \"ImagePrompt\"\n",
    "        }]\n",
    "    }\n",
    "result = image_prompt(params)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# text to image with imageprompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import base64\n",
    "\n",
    "# text to image with imageprompt Example\n",
    "host = \"http://127.0.0.1:8888\"\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "source = open(\"./imgs/s.jpg\", \"rb\").read()\n",
    "def image_prompt(params: dict) -> dict:\n",
    "    \"\"\"\n",
    "    image prompt\n",
    "    \"\"\"\n",
    "    response = requests.post(url=f\"{host}/v2/generation/text-to-image-with-ip\",\n",
    "                             data=json.dumps(params),\n",
    "                             headers={\"Content-Type\": \"application/json\"})\n",
    "    return response.json()\n",
    "\n",
    "params = {\n",
    "    \"prompt\": \"A bear\",\n",
    "    \"image_prompts\": [\n",
    "        {\n",
    "            \"cn_img\": base64.b64encode(source).decode('utf-8'),\n",
    "            \"cn_stop\": 0.6,\n",
    "            \"cn_weight\": 0.6,\n",
    "            \"cn_type\": \"ImagePrompt\"\n",
    "        },{\n",
    "            \"cn_img\": base64.b64encode(image).decode('utf-8'),\n",
    "            \"cn_stop\": 0.6,\n",
    "            \"cn_weight\": 0.6,\n",
    "            \"cn_type\": \"ImagePrompt\"\n",
    "        }\n",
    "    ]\n",
    "}\n",
    "result = image_prompt(params)\n",
    "print(json.dumps(result, indent=4, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# describe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "image = open(\"./imgs/bear.jpg\", \"rb\").read()\n",
    "def describe_image(image: bytes,\n",
    "                   params: dict = {\"type\": \"Photo\"}) -> dict:\n",
    "    \"\"\"\n",
    "    describe-image\n",
    "    \"\"\"\n",
    "    response = requests.post(url=\"http://127.0.0.1:8888/v1/tools/describe-image\",\n",
    "                        params=params,\n",
    "                        files={\n",
    "                            \"image\": image\n",
    "                        },\n",
    "                        timeout=30)\n",
    "    return response.json()\n",
    "\n",
    "describe_image(image=image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
